# -*- coding: utf-8 -*-
# @Time : 2021/3/31 1:11 下午
# @Author : wudeyang
# @email :wudeyang@sjtu.edu.cn
# @Description:
import  os
import  time
from hyperlpr import HyperLPR_plate_recognition
import numpy as np
import cv2
import matplotlib.pyplot as plt
#读入图片

def del_file(path):
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            del_file(c_path)
        else:
            os.remove(c_path)
def plt_display(s):

    # opencv 不显示中文，转成拼音
    dic={"京":"JING", "沪":"HU", "津":"JIN", "渝":"YU", "冀":"JI", "晋":"JIN", "蒙":"MENG", "辽":"LIAO", "吉":"JI", "黑":"HEI",
         "苏":"SU", "浙":"ZHE", "皖":"WAN", "闽":"MIN", "赣":"GAN", "鲁":"LU", "豫":"YU", "鄂":"E", "湘":"XIANG", "粤":"YUE", "桂":"GUI",
    "琼":"QIONG", "川":"CHUAN", "贵":"GUI", "云":"YUN", "藏":"ZANG", "陕":"SHAN", "甘":"GAN", "青":"QING", "宁":"NING", "新":"XIN",
         "港":"GANG","学":"XUE","使":"SHI","警":"JING","澳":"AO","挂":"GUA","军":"JUN","北":"BEI","南":"NAN","广":"GUANG","沈":"SHEN",
    "兰":"LAN","成":"CHENG","济":"JI","海":"HAI","民":"MIN","航":"HANG","空":"KONG"}
    s=dic[s[0]]+' '+s[1:]
    return s


#识别结果
def dection_and_recognition(img,fine_adjustment=False,sizes=(600,500,400),show=False):
    """
    传入包含有一张车辆的图，返回检测和识别的结果
    :param img: opencv 格式BGR的单张图片
    :param Fine_adjustment: 如果得分较低，是否需要精细化调整来尝试提高分数
    :param sizes: 精细化调整最短边的尺寸
    :param show: 是否可视化，default False
    :return: plate,score,[left,top,right,bottom] 如果检测不到车牌，返回固定值 京A00000 0.0 [0,0,0,0]
    """
    h,w,_=img.shape
    plate_rec=HyperLPR_plate_recognition(img)
    # img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # result=[]
    # for _ in plate_rec:

    plate,score,[left,top,right,bottom]=plate_rec[0] if len(plate_rec)>0 else ["京A00000" ,0.0, [0,0,0,0]]


    # 如果分数小于固定值，则重新缩放测试
    if score<0.9 and fine_adjustment:
        for size in sizes:
            h,w,_=img.shape
            ratio=size/min(h,w)
            img_new=cv2.resize(img,None,fx=ratio,fy=ratio,interpolation=6)
            _=HyperLPR_plate_recognition(img_new)
            if len(_)==0:
                continue
            plate_tmp,score_tmp,[left_tmp,top_tmp,right_tmp,bottom_tmp]=_[0]
            if score_tmp>0.9:
                plate,score,[left,top,right,bottom]=plate_tmp,score_tmp,[left_tmp,top_tmp,right_tmp,bottom_tmp]
                left,top,right,bottom=max(int(left/ratio),0),max(int(top/ratio),0),min(int(right/ratio),w-1),min(int(bottom/ratio),h-1)
                break

    Four_vertices_locations=np.array(((left,top),(right,top),(right,bottom),(left,bottom)))
        # print(Four_vertices_locations)
    if show:

        img=cv2.drawContours(img, [Four_vertices_locations], -1, (255, 0, 0), 3)
        plt.imshow(img)

    if show:
        plt.show()

    return plate,score,[left,top,right,bottom]
if __name__=="__main__":
    # 测试FPS
    # path_="/home/wudeyang/LPR/test_data/"
    # start = time.time()
    # for file in os.listdir(path_):
    #     image = cv2.imread(os.path.join(path_,file))
    #     plate,score,[left,top,right,bottom]=dection_and_recognition(image,fine_adjustment=True,show=False)
    #
    # end = time.time()
    # print(end-start)


    image = cv2.imread("/home/wudeyang/LPR/test_data/image/14.jpg")
    ratio=1.0
    image=cv2.resize(image,None,fx=ratio,fy=ratio,interpolation=6)
    result=dection_and_recognition(image,fine_adjustment=True,show=True)
    print(result )

